#include "previewhtmlwidget.h"

PreviewHTMLWidget::PreviewHTMLWidget()
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
readSettings();

QObject::connect( printToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( print_file() ) );
QObject::connect( pdfToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( print_pdf() ) );
                  
}

PreviewHTMLWidget::~PreviewHTMLWidget()
{
}

void PreviewHTMLWidget::set_text( QString & text )
{
textBrowser->setAcceptRichText(true);
textBrowser->setHtml(text);
}

void PreviewHTMLWidget::closeEvent( QCloseEvent * e )
{
textBrowser->clear();
writeSettings();
e->accept();
}

void PreviewHTMLWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("PreviewHTMLWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.endGroup();
}

void PreviewHTMLWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);                
settings.beginGroup( QLatin1String("PreviewHTMLWidget") );
resize( settings.value( QLatin1String("size"), QSize( 700, 500 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void PreviewHTMLWidget::print_file()
{
QTextDocument * document = textBrowser->document();
QPrinter printer;
QPrintDialog * dialog = new QPrintDialog( &printer, this );
    if (dialog->exec() != QDialog::Accepted)
    {
    return;
    }
document->print( &printer );
}

void PreviewHTMLWidget::print_pdf()
{
QSplashScreen * s = new QSplashScreen( create_splash_image() );
QString f = QFileDialog::getSaveFileName( this,
                                          tr("Create PDF File"),
                                          QString(),
                                          QLatin1String("*.pdf") );
    if ( true != f.isEmpty() )
    {
        if ( true == QFileInfo(f).suffix().isEmpty() )
        {
        f.append(".pdf");
        }
    QPrinter printer( QPrinter::HighResolution );
    printer.setOutputFormat( QPrinter::PdfFormat );
    printer.setOutputFileName(f);
        if ( NULL != textBrowser->document() )
        {
        s->show();
        s->showMessage( tr("Creating PDF File ...") );
        qApp->processEvents();        
        textBrowser->document()->print(&printer);
        s->close();
        }
    }
delete s;
}

QPixmap PreviewHTMLWidget::create_splash_image()
{
QPixmap p;
bool found_in_cache = false;
found_in_cache = QPixmapCache::find( QLatin1String("splash_image"), p );
    if ( true != found_in_cache )
    {
    QString path = QCoreApplication::applicationDirPath();
    path.append ( QDir::separator() );
    path.append( QLatin1String("splash_image.png") );
    QFile f(path);
    bool ok = false;
    ok = f.open(QIODevice::ReadOnly);
        if ( true != ok )
        {
        p = QPixmap(":/binary/empty_splash.png");
        return p;
        }
    p = QPixmap(path);
        if ( true == p.isNull() )
        {
        p = QPixmap(":/binary/empty_splash.png");
        }
    QPixmapCache::insert( QLatin1String("splash_image"), p );
    }
return p;
}
